package info.grid.despierto.group;

import info.grid.despierto.MultiButtonListView;
import info.grid.despierto.R;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;


public class GroupAdapter extends ArrayAdapter<GroupRow> {
	private LayoutInflater inflater;
	private int resId;

	public GroupAdapter(Context ctx, int resId, List<GroupRow> items) {
		super(ctx, resId, items);
		this.resId = resId;
		this.inflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		MultiButtonListView list = null;
		try {
			list = (MultiButtonListView) parent;
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (convertView == null) {
			convertView = inflater.inflate(resId, null);
			holder = new ViewHolder();
			holder.titleView = (TextView) convertView.findViewById(R.id.titleText);
			holder.groupOnoff = (CheckBox) convertView.findViewById(R.id.group_onoff);
			holder.everudayView = (TextView) convertView.findViewById(R.id.everyDay_view);
			holder.everudayView = (TextView) convertView.findViewById(R.id.everyDay_view);
			holder.sundayView = (TextView) convertView.findViewById(R.id.sunday_view);
			holder.mondayView = (TextView) convertView.findViewById(R.id.monday_view);
			holder.tuesdayView = (TextView) convertView.findViewById(R.id.tuesday_view);
			holder.wednesdayView = (TextView) convertView.findViewById(R.id.wednesday_view);
			holder.thursdayView = (TextView) convertView.findViewById(R.id.thursday_view);
			holder.fridayView = (TextView) convertView.findViewById(R.id.friday_view);
			holder.saturdayView = (TextView) convertView.findViewById(R.id.saturday_view);
			holder.groupOnoff.setOnClickListener(list);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		GroupRow group = this.getItem(position);
		holder.titleView.setText(group.getName());
		if(group.isOnoff()){
			holder.groupOnoff.setChecked(false);
		}else{
			holder.groupOnoff.setChecked(true);
		}
		holder.groupOnoff.setTag(position);
		holder.everudayView.setText(group.getEve());
		holder.sundayView.setText(group.getSun());
		holder.mondayView.setText(group.getMon());
		holder.tuesdayView.setText(group.getTue());
		holder.wednesdayView.setText(group.getWed());
		holder.thursdayView.setText(group.getThu());
		holder.fridayView.setText(group.getFri());
		holder.saturdayView.setText(group.getSat());

		return convertView;
	}

	private static class ViewHolder {
		public TextView titleView;
		public CheckBox groupOnoff;
		public TextView everudayView;
		public TextView sundayView;
		public TextView mondayView;
		public TextView tuesdayView;
		public TextView wednesdayView;
		public TextView thursdayView;
		public TextView fridayView;
		public TextView saturdayView;

	}
}